package com.ggh.jinjilive.remotedata;

import android.text.TextUtils;

import com.blankj.utilcode.util.AppUtils;
import com.ggh.jinjilive.common.MerchantSession;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class BaseInterceptor implements Interceptor {

    private Map<String, String> headers = new HashMap<>();

    public BaseInterceptor() {

    }

    public BaseInterceptor(Map<String, String> headers) {
        this.headers = headers;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request.Builder builder = chain.request()
                .newBuilder();
        if(MerchantSession.getInstance().isLogin() && !TextUtils.isEmpty(MerchantSession.getInstance().getToken())){
            headers.clear();
            headers.put("token", MerchantSession.getInstance().getToken());
        }else {
            headers.clear();
        }
        headers.put("platform","android");
        headers.put("version", AppUtils.getAppVersionName());
        if (headers != null && headers.size() > 0) {
            Set<String> keys = headers.keySet();
            for (String headerKey : keys) {
                builder.addHeader(headerKey, headers.get(headerKey)).build();
            }
        }
        //请求信息
        return chain.proceed(builder.build());
    }
}
